<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom:50px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表:优秀实践，copy domain修改列表-->
    <el-table :data="pageData.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="100" sortable>
      </el-table-column>
      <el-table-column prop="headImg" label="头像" width="120" sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="120" sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话号码" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="150" sortable>
      </el-table-column>
      <!--<el-table-column prop="state" label="状态" width="120" sortable :formatter="stateFormatter">-->
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template scope="scope">
          <span style="color: green" v-if="scope.row.state===0">正常</span>
          <span style="color: red" v-else-if="scope.row.state===-1">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="120" sortable>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="query.currentPage"
                     :page-size="query.pageSize" :total="pageData.total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="userVisible" :close-on-click-modal="false">
      <el-form :model="user" label-width="80px" :rules="userRules" ref="userForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="headImg">
          <el-input v-model="user.headImg" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="user.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="user.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="user.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="user.state">
            <el-radio class="radio" :label="0">正常</el-radio>
            <el-radio class="radio" :label="-1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="userVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="userLoading">保存</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>
export default {
  data() {
    return {
      pageData: {
        total: 0,//分页条
        data: []//数据表格
      },
      query: {
        keyword: '',
        currentPage: 1,//分页条
        pageSize: 10 //分页条
      },
      listLoading: false,
      sels: [],//列表选中列
      title: '',
      userVisible: false,//新增界面是否显示
      userLoading: false,
      userRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入真实年龄', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系方式', trigger: 'blur'}
        ]
      },
      user: {
        id: null,
        username: '',
        email: '',
        phone: '',
        password: '',
        age: null,
        state: null,
        createTime: '',
        headImg: ''
      }
    }
  },
  methods: {
    search() {
      //修改页码
      this.query.currentPage = 1;
      //改查询条件=双向绑定输入时自动改变，只需要重新查询一次就ok
      this.getUsers();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getUsers();
    },
    //获取用户列表
    getUsers() {
      this.listLoading = true; //加载条
      this.$http.post("/user", this.query)
          .then(result => {
            this.pageData = result.data;
            this.listLoading = false; //去除加载条
          })
          .catch(result => {
            alert("获取用户列表失败");
          })
    },
    //删除
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "修改员工激活状态"
      this.userVisible = true;//弹出修改框
      this.user = Object.assign({}, row);//数据回显
    },
    //保存
    saveSubmit: function () {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.userLoading = true;
            let para = Object.assign({}, this.user)
            this.$http.put("/user", para)
                .then((result) => {
                  this.userLoading = false;
                  result = result.data
                  if (result.success) {
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    })
                    this.$refs['userForm'].resetFields();//重置表单
                    this.userVisible = false;//隐藏模态框
                    this.getUsers();//加载当前数据
                  } else {
                    this.$message({
                      message: result.msg,
                      type: 'error'
                      //保存成功 则提交表单 保存失败 可修改后重新提交
                    });
                  }
                });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);//[34,27]
      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/user", ids).then((result) => {
          this.listLoading = false;
          result = result.data
          if (result.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: result.msg,
              type: 'error'
            })
          }
          this.handleCurrentChange(1)
        });
      }).catch(() => {
      });
    }
  },
  mounted() {
    this.getUsers();
  }
}

</script>

<style scoped>

</style>